package com.ocom.payconfig.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.payconfig.EntityXfDiscountConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.XfDiscountConfigService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/7/21
 **/
@RestController
public class ConsDiscountConfigController {

    @Resource
    private RedisUtil redisUtil;


    @Autowired
    XfDiscountConfigService xfDiscountConfigService;

    @PostMapping(PayConfigUrl.Url_getConsDiscountConfig)
    public Result getConsDiscountConfig(Long comId, Integer accountType,
                                        @RequestParam(required = false, defaultValue = "0") Integer consTimeZone) {
        return ResultUtil.success(xfDiscountConfigService.getConsDiscountConfig(comId, accountType, consTimeZone));
    }

    @PostMapping(PayConfigUrl.Url_setConsDiscountConfig)
    public Result setConsDiscountConfig(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig) {
        EntityXfDiscountConfig en = xfDiscountConfigService.setConsDiscountConfig(entityXfDiscountConfig.getComId(),
                entityXfDiscountConfig.getAccTypeId(),
                entityXfDiscountConfig.getConsType(),
                entityXfDiscountConfig.getMealNo(),
                entityXfDiscountConfig.getConsTimes(),
                entityXfDiscountConfig.getConsTimeZone(),
                entityXfDiscountConfig.getMerchId(),
                entityXfDiscountConfig
        );

//		String canKey="xf_discount_config#comId:"+entityXfDiscountConfig.getComId()+"#accTypeId:"+entityXfDiscountConfig.getAccTypeId()+"#consType:"+entityXfDiscountConfig.getConsType()+"#mealNo:"+entityXfDiscountConfig.getMealNo()  +"#merchId:"+ entityXfDiscountConfig.getMerchId()+"#";
        redisUtil.dels("xf_discount_config#comId:" + entityXfDiscountConfig.getComId());

//		String existKey="exist:" + canKey;
//		redisUtil.del(existKey);
//
//		String canKey1 = "ConsTimeZone#comId:"+entityXfDiscountConfig.getComId() ;

        redisUtil.dels("ConsTimeZone#comId:" + entityXfDiscountConfig.getComId());


        if (en != null) {
            return ResultUtil.success();
        }


        return ResultUtil.error(ResultEnum.Err_3014);
    }

    @PostMapping(PayConfigUrl.Url_setConsDiscountConfigB)
    public Result setConsDiscountConfigB(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig) {

        Result result = xfDiscountConfigService.setConsDiscountConfigB(entityXfDiscountConfig.getComId(), entityXfDiscountConfig);

        redisUtil.dels("xf_discount_config#comId:" + entityXfDiscountConfig.getComId());

        redisUtil.dels("ConsTimeZone#comId:" + entityXfDiscountConfig.getComId());

        return result;
    }

    @PostMapping(PayConfigUrl.Url_removeConsDiscountConfig)
    public Result removeConsDiscountConfig(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig) {
        Boolean b = xfDiscountConfigService.removeConsDiscountConfig(entityXfDiscountConfig.getComId(),
                entityXfDiscountConfig.getAccTypeId(),
                entityXfDiscountConfig.getConsType(),
                entityXfDiscountConfig.getMealNo(),
                entityXfDiscountConfig.getConsTimes(),
                entityXfDiscountConfig.getConsTimeZone(),
                entityXfDiscountConfig.getMerchId(),
                entityXfDiscountConfig.getDiscountValue());

//		String canKey=
//				"xf_discount_config#comId:"+ entityXfDiscountConfig.getComId()+ "#accTypeId:"+entityXfDiscountConfig.getAccTypeId()+ "#consType:"+entityXfDiscountConfig.getConsType()+ "#mealNo:"+entityXfDiscountConfig.getMealNo()+"#merchId:"+ entityXfDiscountConfig.getMerchId()+"#";
//		redisUtil.del(canKey);
        redisUtil.dels("xf_discount_config#comId:" + entityXfDiscountConfig.getComId());
//
//		String existKey="exist:" + canKey;
//		redisUtil.del(existKey);
//
//		String canKey1 = "ConsTimeZone#comId:"+entityXfDiscountConfig.getComId() ;
//
//		redisUtil.del(canKey1);
        redisUtil.dels("ConsTimeZone#comId:" + entityXfDiscountConfig.getComId());

        if (b) {
            return ResultUtil.success();
        }

        return ResultUtil.error(ResultEnum.Err_3016);
    }

    @PostMapping(PayConfigUrl.Url_removeConsDiscountConfigB)
    public Result removeConsDiscountConfigB(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig) {
        Boolean b = xfDiscountConfigService.removeConsDiscountConfig(entityXfDiscountConfig.getComId(),
                entityXfDiscountConfig.getAccTypeId(),
                entityXfDiscountConfig.getConsType(),
                entityXfDiscountConfig.getMealNo(),
                entityXfDiscountConfig.getConsTimes(),
                entityXfDiscountConfig.getConsTimeZone(),
                null,
                entityXfDiscountConfig.getDiscountValue());

        redisUtil.dels("xf_discount_config#comId:" + entityXfDiscountConfig.getComId());

        redisUtil.dels("ConsTimeZone#comId:" + entityXfDiscountConfig.getComId());

        if (b) {
            return ResultUtil.success();
        }

        return ResultUtil.error(ResultEnum.Err_3016);
    }
}
